<?php
require_once(LIB.'/class_Categories.php');
//require_once(LIB.'/class_Content.php');
if (!defined('TB_CATEGORIES')){ define('TB_CATEGORIES','categories');}
class manage_request_controller{
    
    private $categories;
    private $structure;
    function __construct() {
        $categories=new categories();
        $categories->get_full_list();
        $cat_structure=$categories->open_categories_tree(0,0);
        array_shift($cat_structure);   
        $this->structure=$cat_structure;
        $this->categories=$categories;                
    }
    
    public function exec_index(){
       $prolayout=  SESSION_value('prolayout');
        //echo $prolayout;
       if($prolayout=='1'){
            $content=$this->show_index_pro();
        }else{
            $content=$this->show_index_normal();
        }
       
       return array('content'=>$content);
    }

    public function exec_create(){
       $prolayout=  SESSION_value('prolayout');

       $subject=POST_value('subject','');
       $detail=POST_value('detail','');
       
       $status=  GET_value('s','');
       //echo $prolayout;

       if(($subject=="")||($detail=="")){
            $content = "<div style='margin:1px;padding:1px;background:#eee;border:1px solid #ddd;'> Please fill the require fields (*)</div>";
            if($prolayout=='1'){
                $content.=$this->show_create_pro();
            }else{
                $content.=$this->show_create_normal();
            }

        }else{
            if ($status=='submit'){
                $message = Servicedesk :: create_ticket();
                //print_r($_FILES['file']);
                $content = "<div style='margin:1px;padding:1px;background:#eee;border:1px solid #ddd;'>".$message."</div>";
                if($prolayout=='1'){
                    $content.=$this->show_create_pro();
                }else{
                    $content.=$this->show_create_normal();
                }
            }else{
                if($prolayout=='1'){
                    $content.=$this->show_create_pro();
                }else{
                    $content.=$this->show_create_normal();
                }           
            }
        }
       

       
       return array('content'=>$content);
    }
    
    private function show_create_pro(){
        global $dbdefault;
        //$user_id=  SESSION_value('user_id');
        $user_name=  SESSION_value('user_name');
        //lay thong tin user tao
        $queryu=sprintf("SELECT * FROM users where user_name=%s",  qs($user_name));
        $resultu=mysql_query($queryu,$dbdefault);
        $ucreate = mysql_fetch_array($resultu);
        
        //Lấy group tecnical
        $query=sprintf("SELECT * FROM users where is_technical='1'");
        $result = mysql_query($query,$dbdefault);
        $tech = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $tech[]=$arr;
        }
        //Lay all customer
        $queryc=sprintf("SELECT idcustomers, namecustomer FROM customers where cstatus='1'");
        $resultc = mysql_query($queryc,$dbdefault);
        $customer = array(); 
        while($arr=  mysql_fetch_assoc($resultc)){
            $customer[]=$arr;
        }
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_request/view/create_form_pro.html');
        $posts_tpl->set('user_name',$user_name);
        $posts_tpl->set('tech',$tech); 
        $posts_tpl->set('ucreate',$ucreate); 
        $posts_tpl->set('customer',$customer); 
        $posts_tpl->set('categories',$this->categories->categories); 
        $posts_tpl->set('structure',$this->structure);  
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_right_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_request/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }
    
    private function show_create_normal(){
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_request/view/create_form_normal.html');
//        $posts_tpl->set('row',$row);
//        $posts_tpl->set('posts',$posts); 

        $m_content=$posts_tpl->fetch();
        $menu = $this->show_right_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_request/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
        
    }


    private function show_index_pro(){
        global $dbdefault;
        
        $item_per_page = 25;        
        $page_num = GET_value('p',1);
        $limit = ($page_num-1)*$item_per_page;
        $offset = $item_per_page+1;
        
        //giới hạn điều kiện
        $where="";
                
        
        $qry="SELECT count(*) AS total FROM tickets where tstatus=1".$where; 
        $res_total=mysql_query($qry,$dbdefault) or die(mysql_error()."<br />".$qry);
        $total=mysql_result($res_total,0,'total');
        $num_pages=ceil($total/$item_per_page);

        $query=  sprintf("SELECT a.*, b.statusname FROM tickets a LEFT JOIN ticketstatus b ON a.`ticketstatus`=b.`idticketstatus` order by a.lastupdate desc");
        $result = mysql_query($query,$dbdefault);
        $tickets = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $tickets[]=$arr;
        }
        
        
        //Lay all customer
        $queryc=sprintf("SELECT idcustomers, namecustomer FROM customers where cstatus='1'");
        $resultc = mysql_query($queryc,$dbdefault);
        $customer = array(); 
        while($arr=  mysql_fetch_assoc($resultc)){
            $customer[]=$arr;
        }
        //Lấy group tecnical
        $queryt=sprintf("SELECT user_id, user_name FROM users where is_technical='1'");
        $resultt = mysql_query($queryt,$dbdefault);
        $tech = array(); 
        while($arr=  mysql_fetch_assoc($resultt)){
            $tech[]=$arr;
        }
        
        //Count NEW ticket
        $qry_n="SELECT count(*) AS new FROM tickets where tstatus=1 and ticketstatus=1"; 
        $res_total_n=mysql_query($qry_n,$dbdefault) or die(mysql_error()."<br />".$qry);
        $new=mysql_result($res_total_n,0,'new');
        //Count OPEN ticket
        $qry_o="SELECT count(*) AS open FROM tickets where tstatus=1 and ticketstatus=2"; 
        $res_total_o=mysql_query($qry_o,$dbdefault) or die(mysql_error()."<br />".$qry);
        $open=mysql_result($res_total_o,0,'open');
        //Count RESOVLED ticket
        $qry_r="SELECT count(*) AS resolved FROM tickets where tstatus=1 and ticketstatus=3"; 
        $res_total_r=mysql_query($qry_r,$dbdefault) or die(mysql_error()."<br />".$qry);
        $resolved=mysql_result($res_total_r,0,'resolved');        
        //Count PENDING ticket
        $qry_p="SELECT count(*) AS pending FROM tickets where tstatus=1 and ticketstatus=4"; 
        $res_total_p=mysql_query($qry_p,$dbdefault) or die(mysql_error()."<br />".$qry);
        $pending=mysql_result($res_total_p,0,'pending');          
        //Count CLOSE ticket
        $qry_c="SELECT count(*) AS closed FROM tickets where tstatus=1 and ticketstatus=5"; 
        $res_total_c=mysql_query($qry_c,$dbdefault) or die(mysql_error()."<br />".$qry);
        $closed=mysql_result($res_total_c,0,'closed'); 
        //Count DELETED ticket
        $qry_d="SELECT count(*) AS deleted FROM tickets where tstatus=0"; 
        $res_total_d=mysql_query($qry_d,$dbdefault) or die(mysql_error()."<br />".$qry);
        $deleted=mysql_result($res_total_d,0,'deleted'); 
        
        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_request/view/view_index_pro.html');
        $posts_tpl->set('new',$new);
        $posts_tpl->set('open',$open);
        $posts_tpl->set('resolved',$resolved);
        $posts_tpl->set('pending',$pending);
        $posts_tpl->set('closed',$closed);    
        $posts_tpl->set('deleted',$deleted); 
        
        $posts_tpl->set('tickets',$tickets);
        $posts_tpl->set('customer',$customer); 
        $posts_tpl->set('tech',$tech); 
        $m_content=$posts_tpl->fetch();
        $menu = $this->show_right_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_request/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }

    private function show_index_normal(){
        global $dbdefault;
        
        $item_per_page = 25;        
        $page_num = GET_value('p',1);
        $limit = ($page_num-1)*$item_per_page;
        $offset = $item_per_page+1;
        
        //giới hạn điều kiện
        $where="";
        
        $query=sprintf("select * from ");
        
        
        $qry="SELECT count(*) AS total FROM solutions where sstatus=1".$where; 
        $res_total=mysql_query($qry,$dbdefault) or die(mysql_error()."<br />".$qry);
        $total=mysql_result($res_total,0,'total');
        $num_pages=ceil($total/$item_per_page);

        
        $posts_tpl = new Template(MODULE_ROOT.'/manage_request/view/view_index_normal.html');
//        $posts_tpl->set('row',$row);
//        $posts_tpl->set('posts',$posts); 

        $m_content=$posts_tpl->fetch();
        $menu = $this->show_right_menu();

        $tpl = new Template(MODULE_ROOT.'/manage_request/view/layout.html');
        $tpl ->set('left_menu', $menu);
        $tpl ->set('content', $m_content);

        return $tpl ->fetch(); 
    }


    private function show_right_menu($action=''){
       global $dbdefault;
       //$catid =  GET_value('catid','0');
       $query1="SELECT a.*, b.category_name FROM (SELECT category_id, COUNT(*) AS total FROM solutions 
           WHERE sstatus=1 GROUP BY category_id) a JOIN categories b ON a.category_id=b.`category_id`"; 
       $result = mysql_query($query1,$dbdefault);
        $postsl = array(); 
        while($arr=  mysql_fetch_assoc($result)){
            $postsl[]=$arr;
        }
        
        if (empty($action)) $action =  GET_value('a','index');
        $tpl = new Template(MODULE_ROOT.'/manage_request/view/right_menu.html');

        return $tpl->fetch();
    }
    
}
?>
